package name.luojun.DBhelper.model;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class TableVO {
	private String name;
	private String classname;
	private Map<String, Column> columns;
	private List<String> pk;
	private String pkname;
	private boolean mutable;
	public boolean getMutable() {
		return mutable;
	}
	public String getClassname() {
		return classname;
	}
	public void setClassname(String classname) {
		this.classname = classname;
	}
	public void setMutable(boolean mutable) {
		this.mutable = mutable;
	}
	public String getPkname() {
		return pkname;
	}
	public void setPkname(String pkname) {
		this.pkname = pkname;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Map<String, Column> getColumns() {
		return columns;
	}
	public void setColumns(Map<String, Column> columns) {
		this.columns = columns;
	}
	public List<String> getPk() {
		return pk;
	}
	public void setPk(List<String> pk) {
		this.pk = pk;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((columns == null) ? 0 : columns.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((pk == null) ? 0 : pk.hashCode());
		result = prime * result + ((pkname == null) ? 0 : pkname.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TableVO other = (TableVO) obj;
		if (columns == null) {
			if (other.columns != null)
				return false;
		} else if (!columns.equals(other.columns))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (pk == null) {
			if (other.pk != null)
				return false;
		} else if (!pk.equals(other.pk))
			return false;
		if (pkname == null) {
			if (other.pkname != null)
				return false;
		} else if (!pkname.equals(other.pkname))
			return false;
		return true;
	}
	public String createSql(TableVO before) {
		return null;
	}
	public String createSqlsqlLite() {
		StringBuffer sb = new StringBuffer();
		sb.append("CREATE TABLE " + this.name + "(");
		for (Entry<String, Column> en : columns.entrySet()) {
			sb.append(en.getValue().toString() + ",");
		}
		sb.deleteCharAt(sb.length() - 1);
		sb.append(")");
		return sb.toString();
	}
	public String createSql() {
		StringBuffer sb = new StringBuffer();
		sb.append("CREATE TABLE " + this.name + " ( ");
		for (Entry<String, Column> en : columns.entrySet()) {
			sb.append(en.getValue().toString() + ",");
		}
		if (pk.size() > 0) {
			if (pkname != null) {
				sb.append(" CONSTRAINT ");
				sb.append(pkname);
			}
			sb.append(" PRIMARY KEY ( ");
			for (String s : pk) {
				sb.append(s + ",");
			}
			sb.deleteCharAt(sb.length() - 1);
			sb.append(" ) ");
		}
		sb.deleteCharAt(sb.length() - 1);
		sb.append(" )");
		return sb.toString();
	}
}
